import { rejectReleaseCargoApi } from "@/api/waybillManagement/financeWaybill/releaseCargo";
import FormHook from "@/hook/formHook";
import { message } from "ant-design-vue";
import { Ref } from "vue";
export default function OperationHook(state: { formState: { refuseReason: string }; formDataRef: Ref<any>; emit: any; passData: Ref<Array<ReleaseCargo.ListRows>>; loading: Ref<boolean> }) {
  const { formState, formDataRef, emit, passData, loading } = state;
  const { resetFields } = FormHook({ formState });
  const onFinish = () => {
    const { refuseReason } = formState;
    const params: Parameters<typeof rejectReleaseCargoApi>[0] = {
      refuseReason,
      idList: passData.value.map((t: ReleaseCargo.ListRows) => t.id)
    };
    loading.value = true;
    rejectReleaseCargoApi(params)
      .then((res: any) => {
        const { code } = res;
        if (code === 200) {
          message.success("操作成功!");
          emit("submit");
        }
      })
      .finally(() => (loading.value = false));
  };

  const onFinishFailed = (errorInfo: any) => {
    console.log("Failed:", errorInfo);
  };
  const resetHandle = () => {
    resetFields();
    formDataRef.value.clearValidate();
    emit("update:visible", false);
  };
  return {
    onFinish,
    onFinishFailed,
    resetHandle,
    resetFields
  };
}
